﻿package dt 
{
	import common.Utility;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import game.constants.ConstData
	import game.events.EventData;
	import game.exchangeddata.HashTable;
	/**
	 * ...
	 * @author Nguyen Thi Bich Phuong (Daisy)	
	 */
	public class DragonTigerPanel extends MovieClip
	{
		//interface
		public var txtAskDragonTf:TextField;
		public var txtAskTigerTf:TextField;
		public var askDragonBtn:SimpleButton;
		public var askTigerBtn:SimpleButton;
		public var hisType1:MovieClip;
		public var hisType2:MovieClip;
		public var hisBoard1:Sprite = new Sprite();
		public var hisBoard2:Sprite = new Sprite();
		
		//Business
		private var rowIndType1:int = 0;
		private var colIndType1:int = 0;
		
		private var rowIndType2:int = 0;
		private var colIndType2:int = 0;
		private var rowNum:int = 6;
		private static const DRAGON:String = "1";		
		private static const TIGER:String = "2";		
		
		private static const TIE_1:String = "1";
		private static const TIE_2:String = "2";
		private static const TIE_3:String = "3";
		private static const TIE_4:String = "4";
		private static const TIE_5:String = "5";
		
		private static const TYPE_1:String = "1";
		private static const TYPE_2:String = "2";
		private static const TYPE_3:String = "3";
		private static const TYPE_4:String = "4";
		private static const TYPE_5:String = "5";
		
		private static const RESULT:String = "0";
		
		private static const DRAGON_VALUE:String = "10";
		private static const TIGER_VALUE:String = "20"; 
		
		public  var curAskClick:String;
		
		public function DragonTigerPanel() 
		{
			txtAskDragonTf.mouseEnabled = false;
			txtAskTigerTf.mouseEnabled = false;
			hisType1.addChild(hisBoard1);
			hisType2.addChild(hisBoard2);
			askDragonBtn.addEventListener(MouseEvent.CLICK, getAskType1Hdl);
			askTigerBtn.addEventListener(MouseEvent.CLICK, getAskType2Hdl);			
		}
		/**
		 * @author	Bamboo
		 * @param	hashTableInfo
		 */
		public function initText(hashTableInfo:HashTable):void {	
			this.txtAskTigerTf.text = hashTableInfo.lookupString("asktiger");
			this.txtAskDragonTf.text = hashTableInfo.lookupString("askdragon");
		}
		
		/**
		 * @author	Bamboo
		 */
		public function lock():void {
			askDragonBtn.mouseEnabled = false;
			askTigerBtn.mouseEnabled = false;
		}
		/**
		 * @author	Bamboo
		 */
		public function unLock():void {
			askDragonBtn.mouseEnabled = true;
			askTigerBtn.mouseEnabled = true;
		}
		/**
		 * @author	Bamboo
		 * @param	e
		 */
		private function getAskType1Hdl(e:MouseEvent):void 
		{
			dispatchEvent( new Event( EventData.GET_ASK_PLAYER_BANKER_TYPE_1, true));
			curAskClick =  DragonTigerPanel.DRAGON_VALUE;
		}
		/**
		 * @author	Bamboo
		 * @param	e
		 */
		private function getAskType2Hdl(e:MouseEvent):void 
		{
			dispatchEvent( new Event( EventData.GET_ASK_PLAYER_BANKER_TYPE_2, true));
			curAskClick = DragonTigerPanel.TIGER_VALUE;
		}
		
		/**
		 * @author Bamboo
		 * @param	firstDigit
		 * @param	secDigit
		 * @param	threeDigit
		 * @param	sLoop
		 * @param	lastCode
		 * @param	hisBoard
		 * @return This is movieChip that add to hisboard
		 */
		private function attachIconType1(firstDigit:String, secDigit:String, threeDigit:String, sLoop:String, lastCode:Boolean):MovieClip {
				
			if (firstDigit != DragonTigerPanel.RESULT) {
				
				var hisIcon:MovieClip = MovieClip(new AskDragonTigerIconType1());
				var icon:MovieClip = hisIcon.mc;
				hisIcon.mouseEnabled = false;
				hisIcon.mouseChildren = false;
				hisBoard1.addChild(hisIcon);
				
				hisIcon.x = colIndType1 * 12 - 222 + 6;//13.8
				hisIcon.y = rowIndType1 * 12 - 36  + 6;//22
			
				hisIcon.scaleX = hisIcon.scaleY = 0.8;		
				
				icon.tie_1.visible = false;
				icon.tie_2.visible = false;
				icon.tie_3.visible = false;
				icon.tie_n.visible = false;
				
				
				if (firstDigit == DragonTigerPanel.DRAGON) {
					icon.gotoAndStop("dragon");
				} else if (firstDigit == DragonTigerPanel.TIGER) {
					icon.gotoAndStop("tiger");
				}
				if (sLoop == "Y" && lastCode) {
					hisIcon.gotoAndPlay("loop");
				} else {
					hisIcon.stop();

				}				
				
				if (threeDigit != DragonTigerPanel.RESULT) {
					if (firstDigit != DragonTigerPanel.TIE_4 && firstDigit != DragonTigerPanel.TIE_5) {
						
						if (threeDigit == DragonTigerPanel.TIE_1) { 
							icon.tie_1.visible = true;
						}else if (threeDigit == DragonTigerPanel.TIE_2) {
							icon.tie_1.visible = true;
							icon.tie_2.visible = true;
						}else if (threeDigit == DragonTigerPanel.TIE_3) {
							icon.tie_1.visible = true;
							icon.tie_2.visible = true;
							icon.tie_3.visible = true;
						}else {
							icon.tie_3.visible = true;
							icon.tie_n.visible = true;
							icon.tie_n.text = threeDigit;
						}
						
					} else {
						icon.tie_n.visible = true;
						icon.tie_n.text = threeDigit;
						var format:TextFormat = new TextFormat();
						if (firstDigit == "4") {
							format.color = 0xFF0000;
							icon.tie_n.setTextFormat(format);
						}else {
							format.color = 0x0000FF;
							icon.tie_n.setTextFormat(format);
						}
					}
				}
				return hisIcon
			}
			return null;
			
		}
		/**
		 * @author	Bamboo
		 * @param	currentCode
		 * @param	lastCode
		 * @param	sLoop
		 * @param	type
		 * @param	textCode
		 * @param	hisBoard
		 * @return
		 */
		private function attachIconType2(currentCode:String, lastCode:Boolean, sLoop:String, type:String, textCode:String):MovieClip {
		
				var hisIcon:MovieClip = MovieClip(new AskDragonTigerIconType2());
				var icon:MovieClip = hisIcon.mc;
				hisIcon.mouseEnabled = false;
				hisIcon.mouseChildren = false;
				hisBoard2.addChild(hisIcon);
				
				hisIcon.x = colIndType2 * 6 - 222;
				hisIcon.y = rowIndType2 * 6 - 36;
			    icon.bankerRoute1.visible = false;
				icon.bankerRoute2.visible = false;
				icon.bankerRoute3.visible = false;
				icon.playerRoute1.visible = false;
				icon.playerRoute2.visible = false;
				icon.playerRoute3.visible = false;
				icon.route1.visible = false;
				icon.route2.visible = false;
			
				if(currentCode == DragonTigerPanel.TYPE_1){
					if (type == DragonTigerPanel.TYPE_1)
						icon.bankerRoute1.visible = true;
					else if(type == DragonTigerPanel.TYPE_2)
						icon.bankerRoute2.visible = true;
					else if (type == DragonTigerPanel.TYPE_3)
						icon.bankerRoute3.visible = true;
				}
				else if(currentCode == DragonTigerPanel.TYPE_2) {					
					if (type == DragonTigerPanel.TYPE_1)
						icon.playerRoute1.visible = true;
					else if (type == DragonTigerPanel.TYPE_2)
						icon.playerRoute2.visible = true;
					else if (type == DragonTigerPanel.TYPE_3)
						icon.playerRoute3.visible = true;
				}
				else if (currentCode == DragonTigerPanel.TYPE_4) {
					icon.route1.visible = true;
					icon.route1.text = textCode;
				}
				else if(currentCode == DragonTigerPanel.TYPE_5){
					
					icon.route2.visible = true;
					icon.route3.text = textCode;
					
				}
				if(sLoop == "Y" && lastCode){
					hisIcon.gotoAndPlay("loop");
				}else{
					hisIcon.stop();
				}
				return hisIcon;
		}
		/**
		 * @author Bamboo
		 * @param	hisBoard
		 */
		private function removeHistory(hisBoard:Sprite):void {
			
			var numberChid:int = hisBoard.numChildren;
			if (numberChid> 0)
			{
				for (var j:int = 0; j < numberChid; j++ ) 
				{
					hisBoard.removeChildAt(0);
					
				}
				
			}
		}
		/**
		 * @author	Bamboo
		 * @param	numResultCode
		 * @param	sResultCode
		 * @return
		 */
		private function findLastResult(numResultCode:int, sResultCode:String):int {
			var NumCol:int = int(numResultCode/6);
			var lastNum:int = 0;
			var lastNumCode:String = DragonTigerPanel.RESULT;
			// find last column
			for (var i:int = NumCol; i>0; i--) {
				lastNum = (i-1)*18;
				lastNumCode = sResultCode.substr(lastNum, 1);
				if (lastNumCode != DragonTigerPanel.RESULT) {
					break;
				}
			}
			// parse last column
			for (var j:int = 1; j<=5; j++) {
				var tmp:int = lastNum+3;
				var tmpCode:String = sResultCode.substr(tmp, 1);
				if (tmpCode == lastNumCode) {
					lastNum = tmp;
					// test if tail exists
					if ((tmp+18)<numResultCode*3) {
						if (sResultCode.substr(tmp+18, 1) == lastNumCode) {
							break;
						}
					}
					if ((tmp-18)>0) {
						if (sResultCode.substr(tmp-18, 1) == lastNumCode) {
							break;
						}
					}
				} else {
					break;
				}
			} // if tail goes sideway
			var tmpNum:int = lastNum;
			var findTail:Boolean = false;
			while (tmpNum<((numResultCode*3)-3)) {
				tmpNum = tmpNum+18;
				if (tmpNum<((numResultCode*3)-3)) {
					if (sResultCode.substr(tmpNum, 1) != DragonTigerPanel.RESULT) {
						lastNum = tmpNum;
					}
					findTail = true;
				}
			}
			if (!findTail) {
				tmpNum = lastNum;
				while (tmpNum>15) {
					tmpNum = tmpNum-18;
					if (tmpNum>15) {
						if (sResultCode.substr(tmpNum, 1) == lastNumCode) {
							lastNum = tmpNum;
						} else {
							break;
						}
					}
				}
			}
			return lastNum;
		}
		/**
		 * @author	Bamboo
		 * @param	sResultCode
		 * @param	sLoop
		 */
		public function initAskDragonTigerType1(sResultCode:String, sLoop:String):void {						
			removeHistory(hisBoard1);
			rowIndType1 = 0;
			colIndType1 = 0;
			if (sResultCode.length == 0)
				return;
			// Initialize the location of Result Pane
			var MAX_NUM_CHECKERS:int = 222;
			var LEN_CODE:int = 3;
			var numResultCode = int(sResultCode.substr(0, LEN_CODE));
			var tmpCode = sResultCode.substr(4, sResultCode.length-4);
			
			var offset = 0;
			
			var lastNum:int = findLastResult(numResultCode, tmpCode);
			var numTieBefore1:int = int(sResultCode.substr(LEN_CODE, 1));
			if (numTieBefore1 > 0) {
				//this.attachMovie("movTieBefore","plotResult0_0",this.getNextHighestDepth(),{_x:0, _y:0});
				//trace("numTieBefore1: " + numTieBefore1);
			}
			var lastCode:Boolean;
			var currentCode:String;
			var firstDigit:String;
			var secDigit:String;
			var threeDigit:String;
			
			for (var i:int = 0; i<numResultCode && i<MAX_NUM_CHECKERS; i++) {
				if (offset == lastNum) {
					lastCode = true;
				} else {
					lastCode = false;
				}
				
				currentCode = tmpCode.substr(offset, LEN_CODE);
				firstDigit = currentCode.substr(0, 1);
				secDigit = currentCode.substr(1, 1);
				threeDigit = currentCode.substr(2, 1);
				
				var iconHis:MovieClip = attachIconType1(firstDigit, secDigit, threeDigit, sLoop, lastCode);
				
				if(iconHis != null)
					iconHis.name = i.toString();
					
				offset += LEN_CODE;
				if (rowIndType1 + 1 == rowNum) {
					rowIndType1 = 0;
					colIndType1++;
				} else rowIndType1++;
			}
			
		}
		/**
		 * @author	Bamboo
		 * @param	sResultCode
		 * @param	sLoop
		 */
		public function initAskDragonTigerType2(sResultCode:String, sLoop:String):void {
			removeHistory(hisBoard2);
			colIndType2 = 0;
			rowIndType2 = 0;
			if (sResultCode.length == 0)
				return;
			var MAX_NUM_CHECKERS:Number = 222;
			var LEN_CODE:int = 1;
			var CODE_COUNT:int = 2;
			
			var numResultCode:int = int(sResultCode.substr(0, 3));
			var tmpCode:String = sResultCode.substr(3, numResultCode * 3);
			var offset:int = 3;
			var lastNum:int = findLastResult( numResultCode, tmpCode ) + 3;
			var currentCode:String;
			var lastCode:Boolean;
			
			var testCode:String = " ";
			var iconHis:MovieClip;
			//Type 1
			for( var i:int = 0; i < numResultCode && i < MAX_NUM_CHECKERS; i++ )
			{
				currentCode = sResultCode.substr(offset, LEN_CODE);
				if( offset == lastNum )
					lastCode = true;
				else
					lastCode = false;
				
				
				if(currentCode != DragonTigerPanel.RESULT)
				{
					if (currentCode == DragonTigerPanel.TYPE_4 || currentCode == DragonTigerPanel.TYPE_5)
						testCode = sResultCode.substr(offset + LEN_CODE, CODE_COUNT);
					iconHis = attachIconType2(currentCode, lastCode, sLoop, DragonTigerPanel.TYPE_1, testCode);
					if(iconHis != null)
						iconHis.name = i.toString() + DragonTigerPanel.TYPE_2;
					
				}
				offset += LEN_CODE + CODE_COUNT;
				rowIndType2++;
				if( rowIndType2 > 5 ){
					rowIndType2 = 0;
					colIndType2++;
				}
			}
			//Type 2
			colIndType2 = 0;
			rowIndType2 = 6;
			numResultCode = int(sResultCode.substr(offset, 3));
			offset += 3;
			tmpCode = sResultCode.substr(offset, numResultCode * 3);
			lastNum = findLastResult( numResultCode, tmpCode ) + offset;
			for(i = 0; i < numResultCode && i < MAX_NUM_CHECKERS; i++ )
			{
				currentCode = sResultCode.substr(offset, LEN_CODE);
				if ( offset == lastNum )
					lastCode = true;
				else 
					lastCode = false;
				
				if( currentCode != DragonTigerPanel.RESULT)
				{
					if (currentCode == DragonTigerPanel.TYPE_4 || currentCode == DragonTigerPanel.TYPE_5)
						testCode = sResultCode.substr(offset + LEN_CODE, CODE_COUNT);
					iconHis = attachIconType2(currentCode, lastCode, sLoop, DragonTigerPanel.TYPE_2, testCode);
					if(iconHis != null)
						iconHis.name = i.toString() + DragonTigerPanel.TYPE_2;
					
				}
				offset += LEN_CODE + CODE_COUNT;
				rowIndType2++;
				if( rowIndType2 > 11 ){
					rowIndType2 = 6;
					colIndType2++;
				}
			}
			
			colIndType2 = 38;
			rowIndType2 = 6;
			
			numResultCode = parseInt(sResultCode.substr(offset, 3), 10);
			offset += 3;
			tmpCode = sResultCode.substr(offset, numResultCode * 3);
			lastNum = findLastResult( numResultCode, tmpCode ) + offset;
			for(i = 0; i < numResultCode && i < MAX_NUM_CHECKERS; i++ )
			{
				currentCode = sResultCode.substr(offset, LEN_CODE);
				if ( offset == lastNum )
					lastCode = true;
				else 
					lastCode = false;
				
				if( currentCode != DragonTigerPanel.RESULT)
				{
					if (currentCode == DragonTigerPanel.TYPE_4 || currentCode == DragonTigerPanel.TYPE_5)
						testCode = sResultCode.substr(offset + LEN_CODE, CODE_COUNT);
					iconHis = attachIconType2(currentCode, lastCode, sLoop, DragonTigerPanel.TYPE_3, testCode);
					if(iconHis != null)
						iconHis.name = i.toString() + DragonTigerPanel.TYPE_3;
				}
				offset += LEN_CODE + CODE_COUNT;
				rowIndType2++;
				if( rowIndType2 > 11 ){
					rowIndType2 = 6;
					colIndType2++;
				}
			}
		}
	}

}